package com.classic.common.simple;

import com.classic.common.MultipleStatusView;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

public abstract class AbsActivity extends Ability implements Component.ClickedListener {
    static final int DELAY = 2000;
    MultipleStatusView mMultipleStatusView;
    FloatingActionMenu mFloatingActionMenu;
    FloatingActionButton mLoadingFab;
    FloatingActionButton mEmptyFab;
    FloatingActionButton mErrorFab;
    FloatingActionButton mNoNetworkFab;
    FloatingActionButton mContentFab;
    private final Runnable callback = new Runnable() {
        @Override
        public void run() {
            if (!isTemporary() && null != mMultipleStatusView) {
                mMultipleStatusView.showContent();
            }
        }
    };

    abstract void initView();

    @Override
    protected void onStart(Intent savedInstanceState) {
        super.onStart(savedInstanceState);
        initView();
        mFloatingActionMenu = (FloatingActionMenu) findComponentById(ResourceTable.Id_menu);
        mLoadingFab = (FloatingActionButton) findComponentById(ResourceTable.Id_fab_loading);
        mEmptyFab = (FloatingActionButton) findComponentById(ResourceTable.Id_fab_empty);
        mErrorFab = (FloatingActionButton) findComponentById(ResourceTable.Id_fab_error);
        mNoNetworkFab = (FloatingActionButton) findComponentById(ResourceTable.Id_fab_no_network);
        mContentFab = (FloatingActionButton) findComponentById(ResourceTable.Id_fab_content);
        mLoadingFab.setVisibility(Component.INVISIBLE);
        mEmptyFab.setVisibility(Component.INVISIBLE);
        mLoadingFab.setClickedListener(this);
        mEmptyFab.setClickedListener(this);
        mErrorFab.setClickedListener(this);
        mNoNetworkFab.setClickedListener(this);
        mContentFab.setClickedListener(this);
        mMultipleStatusView.setOnRetryClickListener(mRetryClickListener);
        mMultipleStatusView.setOnViewStatusChangeListener(mViewStatusChangeListener);
        mMultipleStatusView.setStatusHintRes(ResourceTable.Id_status_hint_content);
    }

    @Override
    protected void onStop() {
        if (null != mMultipleStatusView) {
            mMultipleStatusView.removeCallbacks(callback);
        }
        super.onStop();
    }

    void loading() {
        mMultipleStatusView.showLoading();
        mMultipleStatusView.postDelayed(callback, DELAY);
    }

    final Component.ClickedListener mRetryClickListener = new Component.ClickedListener() {
        @Override
        public void onClick(Component component) {
            loading();
        }
    };

    final MultipleStatusView.OnViewStatusChangeListener mViewStatusChangeListener =
            new MultipleStatusView.OnViewStatusChangeListener() {
                /**
                 * 视图状态改变时回调
                 *
                 * @param oldViewStatus 之前的视图状态
                 * @param newViewStatus 新的视图状态
                 */
                @Override
                public void onChange(int oldViewStatus, int newViewStatus) {
                }
            };

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_fab_loading:
                loading();
                break;
            case ResourceTable.Id_fab_empty:
                mMultipleStatusView.setEmptyResId(ResourceTable.Id_empty_retry_view);
                mMultipleStatusView.showEmpty(ResourceTable.String_test_hint_content, "aa", "bb");
                break;
            case ResourceTable.Id_fab_error:
                mMultipleStatusView.setErrorResId(ResourceTable.Id_error_retry_view);
                mMultipleStatusView.showError("自定义错误文本");
                break;
            case ResourceTable.Id_fab_no_network:
                mMultipleStatusView.setNetWorkResId(ResourceTable.Id_no_network_retry_view);
                mMultipleStatusView.showNoNetwork("自定义无网络文本");
                break;
            case ResourceTable.Id_fab_content:
                mMultipleStatusView.showContent();
                break;
        }
        mFloatingActionMenu.toggle(false);
    }
}
